using System;

#if UNITY_EDITOR
using Unity.Mathematics;
using UnityEditor;
#endif

namespace UnityEngine.Rendering.Universal
{
    /// <summary>
    ///  Nirvana项目 镇魂街自定义RenderData以及ShaderData
    /// </summary>
    [Serializable]
    public partial class NVRenderData : ScriptableObject
    {

        public static string NVLibaryPath = "Assets/Shader/";
        public ShaderResources shaders;


        #if UNITY_EDITOR



        [MenuItem("Assets/Create/Rendering/NvRenderData")]
        public static void CreateToUniversalAssets()
        {
            var path = AssetDatabase.GUIDToAssetPath(Selection.assetGUIDs[0]);
            var assets = AssetDatabase.LoadAssetAtPath<UniversalRenderPipelineAsset>(path);
            var nvData = GetDefaultNVRenderData(assets);
            assets.SetNvRenderData(nvData);
        }

        [MenuItem("Assets/Create/Rendering/NvRenderData",true)]
        public static bool CheckAssetsType()
        {

            var path = AssetDatabase.GUIDToAssetPath(Selection.assetGUIDs[0]);
            var assets = AssetDatabase.LoadAssetAtPath<UniversalRenderPipelineAsset>(path);
            return assets != null;
        }
        
        
        internal static NVRenderData GetDefaultNVRenderData(Object rendererData)
        {
            var path = AssetDatabase.GetAssetPath(rendererData);
            
            var nvRenderData  = AssetDatabase.LoadAssetAtPath<NVRenderData>(path);
            if (nvRenderData == null)
            {
                nvRenderData = ScriptableObject.CreateInstance<NVRenderData>();
                nvRenderData.name = "NVRenderData";

            }
            AssetDatabase.AddObjectToAsset(nvRenderData, rendererData);
            AssetDatabase.SaveAssets();
            ResourceReloader.ReloadAllNullIn(nvRenderData, NVRenderData.NVLibaryPath);
            return nvRenderData;
        }


        private void OnEnable()
        {
            ResourceReloader.ReloadAllNullIn(this, NVRenderData.NVLibaryPath);
        }
#endif
        
        [Serializable, ReloadGroup]
        public partial class ShaderResources
        {
            
            [Reload("UI/CustomDefaultUI.shader"),SerializeField]
            public Shader CustomUIDedfultShader;
                                
            [Reload("UI/UILinearToGamma.shader"),SerializeField]
            public Shader UILinearToGammaShader;

            [Reload("UI/UIEffect.shader"),SerializeField]
            public Shader UIEffect;
        }
    }
    
    
    
    
    
    public partial class UniversalRenderPipelineAsset : RenderPipelineAsset, ISerializationCallbackReceiver
    {
        
        // GUI写在 SerializedUniversalRenderpipelineAssets中 
        
        //分离RT分辨率
        [SerializeField] private Vector2Int m_SceneRes;
        [SerializeField] private Vector2Int m_BlurCanvasRes;
        [SerializeField] private Vector2Int m_UICanvasRes;
        [SerializeField] private bool m_useCustomCanvasRes;
        [SerializeField] private bool m_useBlurCanvas;
        [SerializeField] private bool m_useSceneRes;
        [SerializeField] private int m_blurQuality ;
        [SerializeField] private bool m_IsDistortionEnable;
        //后处理控制
        [SerializeField] private bool m_useBloom;

        public bool UseBloom
        {
            get{return m_useBloom;}
            set{m_useBloom = value;}
        }

        public bool TryGetRendererFeature<T>(out T feature) where T : ScriptableRendererFeature
        {
            return scriptableRendererData.TryGetRendererFeature(out feature);
        }

        public bool IsDistortionEnable
        {
            get => m_IsDistortionEnable;
            set => m_IsDistortionEnable = value;
        }
        
        public int BlurQuality
        {
            get { return m_blurQuality; }
            set { m_blurQuality = value; }
        }
        
        public Vector2Int SceneRes
        {
            get { return m_SceneRes; }
            set { m_SceneRes = value; }
        }
        
        public Vector2Int UICanvasRes
        {
            get { return m_UICanvasRes; }
            set { m_UICanvasRes = value; }
        }
        public Vector2Int BlurCanvasRes
        {
            get { return m_BlurCanvasRes; }
            set { m_BlurCanvasRes = value; }
        }


        public bool UseCustomBlurCanvasRes
        {
            get => m_useBlurCanvas;
            set => m_useBlurCanvas = value;
        }

        public bool UseCustomCanvasRes
        {
            get => m_useCustomCanvasRes;
            set => m_useCustomCanvasRes = value;
        }

        public bool UseSceneRes
        {
            get => m_useSceneRes;
            set => m_useSceneRes = value;
        }

        public void SetMainLightShadowResolution(ShadowResolution resolution)
        {
            m_MainLightShadowmapResolution = resolution;
        }

        public void EnableMainLightShadow(bool enable)
        {
            supportsMainLightShadows = enable;  
        }
    }
}

